//
//  HHFormTextViewCell.m
//  Pod
//
//  Created by chun.chen on 2018/3/2.
//  Copyright © 2018年 cc. All rights reserved.
//  表单文字类型 cell

#import "HHFormTextViewCell.h"
#import "HHFormSDKPublic.h"
#import "XLFormRowDescriptor.h"
#import "XLFormTextView.h"
#import "XLFormViewController.h"
#import "UIView+XLFormAdditions.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#import <Masonry/Masonry.h>
#pragma clang diagnostic pop

@interface HHFormTextViewCell ()<UITextViewDelegate>

@property (nonatomic, strong) XLFormTextView *textView;
@property (nonatomic, strong) UILabel *titleLab;

@end

@implementation HHFormTextViewCell
+ (void)load
{
    [XLFormViewController.cellClassesForRowDescriptorTypes setObject:[HHFormTextViewCell class] forKey:HHFormRowDescriptorTypeTextView];
}
#pragma mark - XLFormDescriptorCell
+ (CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor
{
    return 135.f;
}
-(void)configure
{
    [super configure];
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [self.contentView addSubview:self.asteriskLab];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.textView];
    
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(14);
    }];
    [self.asteriskLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.titleLab.mas_left).offset(-1);
        make.top.mas_equalTo(14);
    }];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLab);
        make.top.mas_equalTo(40);
        make.bottom.mas_equalTo(self.contentView);
        make.right.mas_equalTo(-15);
    }];
}
-(void)update
{
    [super update];
    //title
    if (self.rowDescriptor.titleColor) {
        self.titleLab.textColor = self.rowDescriptor.titleColor;
    }
    if (self.rowDescriptor.titleAlignment) {
        self.titleLab.textAlignment = self.rowDescriptor.titleAlignment;
    }
    if (self.rowDescriptor.titleLeft>0) {
        [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.rowDescriptor.titleLeft);
        }];
    }
    self.titleLab.text = self.rowDescriptor.title;
    self.asteriskLab.hidden = !self.rowDescriptor.required;
    self.asteriskLab.text = self.rowDescriptor.required?@"*":@"";
    //text
    self.textView.text = self.rowDescriptor.value;
    [self.textView setEditable:!self.rowDescriptor.isDisabled];
    self.textView.textColor  = self.rowDescriptor.isDisabled ? BCComConfig.config.littleBlackColor : BCComConfig.config.blackColor;
    if (self.rowDescriptor.textFont) {
        self.textView.font = self.rowDescriptor.textFont;
    }
    if (self.rowDescriptor.textColor) {
        self.textView.textColor = self.rowDescriptor.textColor;
    }
    if (self.rowDescriptor.textPlaceholder) {
        self.textView.placeholder = self.rowDescriptor.textPlaceholder;
    }
}

-(BOOL)formDescriptorCellCanBecomeFirstResponder
{
    return (!self.rowDescriptor.isDisabled);
}

-(BOOL)formDescriptorCellBecomeFirstResponder
{
    return [self.textView becomeFirstResponder];
}

-(void)highlight
{
    [super highlight];
}

-(void)unhighlight
{
    [super unhighlight];
    [self.formViewController updateFormRow:self.rowDescriptor];
}

-(BOOL)showBottomSplitLine {
    return YES;
}

#pragma mark - UITextViewDelegate

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.formViewController beginEditing:self.rowDescriptor];
    return [self.formViewController textViewDidBeginEditing:textView];
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    if ([self.textView.text length] > 0) {
        self.rowDescriptor.value = self.textView.text;
    } else {
        self.rowDescriptor.value = nil;
    }
    [self.formViewController endEditing:self.rowDescriptor];
    [self.formViewController textViewDidEndEditing:textView];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return [self.formViewController textViewShouldBeginEditing:textView];
}

-(void)textViewDidChange:(UITextView *)textView{
    int maxLength = self.rowDescriptor.textMaxLength;
    if (maxLength > 0) {
        if (self.textView.text.length > maxLength) {
            self.textView.text = [self.textView.text substringWithRange:NSMakeRange(0, maxLength)];
        }
    }
    
    if ([self.textView.text length] > 0) {
        self.rowDescriptor.value = self.textView.text;
    } else {
        self.rowDescriptor.value = nil;
    }
    [self.formViewController textViewDidChange:textView];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    // Otherwise, leave response to view controller
    return [self.formViewController textView:textView shouldChangeTextInRange:range replacementText:text];
}
#pragma mark -Getter
-(XLFormTextView *)textView
{
    if (!_textView) {
        _textView = [[XLFormTextView alloc] init];
        _textView.font = BCComConfig.config.bcFont(16);
        _textView.placeHolderLabel.font = BCComConfig.config.bcFont(16);
        _textView.textColor = BCComConfig.config.blackColor;
        _textView.delegate = self;
        _textView.keyboardType = UIKeyboardTypeDefault;
    }
    return _textView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.translatesAutoresizingMaskIntoConstraints = NO;
        _titleLab.font = BCComConfig.config.bcFont(16);
        _titleLab.textColor = BCComConfig.config.blackColor;
    }
    return _titleLab;
}
@end
